/**
 * 
 */
package com.manage.action;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;


import com.action.AbstractAction;
import com.manage.cache.OrderConfigManager;
import com.manage.cache.CpAppInfoManager;
import com.manage.cache.CpInfoManager;
import com.manage.model.OrderConfig;
import com.manage.model.CpAppInfo;
import com.manage.model.CpInfo;


public class OrderConfigAction extends AbstractAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private Logger logger;
	private List<CpInfo> cplist;
	private List<CpAppInfo> applist;
	private CpInfo cpinfo;
	private List<OrderConfig> list;
	public OrderConfigAction()
	{
		logger = Logger.getLogger(this.getClass());
	}
	
	public String execute()
	{
		return SUCCESS;
	}
	
	public String cpshow()
	{
		cplist = CpInfoManager.getInstance().getCpInfos();
		list = OrderConfigManager.getInstance().getOrderConfigByType(0,-1);
		return SUCCESS;
	}
	
	public String cpsave()
	{
		cplist = CpInfoManager.getInstance().getCpInfos();
		list = new ArrayList<OrderConfig>();
		for(int i = 0;i<cplist.size();i++)
		{
			OrderConfig oc = new OrderConfig();
			oc.setCpid(cplist.get(i).getId());
			oc.setApknumber("");
			int percentage = 100;
			if(getRequest().getParameter("percentage_"+cplist.get(i).getId())!=null)
			{
				try{
					percentage = Integer.parseInt(getRequest().getParameter("percentage_"+cplist.get(i).getId()));
				}catch(Exception ex){}
			}
			oc.setPercentage(percentage);
			list.add(oc);
		}
		OrderConfigManager.getInstance().saveOrderConfig(list, 0,-1);
		return SUCCESS;
	}
	
	public String appshow()
	{
		cplist = CpInfoManager.getInstance().getCpInfos();
		cpinfo = CpInfoManager.getInstance().getCpInfo(cpinfo.getId());
		applist = CpAppInfoManager.getInstance().getCpAppInfos(cpinfo.getId());
		list = OrderConfigManager.getInstance().getOrderConfigByType(1,cpinfo.getId());
		return SUCCESS;
	}
	
	public String appsave()
	{
		cpinfo = CpInfoManager.getInstance().getCpInfo(cpinfo.getId());
		applist = CpAppInfoManager.getInstance().getCpAppInfos(cpinfo.getId());
		list = new ArrayList<OrderConfig>();
		for(int i = 0;i<applist.size();i++)
		{
			OrderConfig oc = new OrderConfig();
			oc.setCpid(cpinfo.getId());
			oc.setApknumber(applist.get(i).getAppnumber());
			int percentage = 100;
			if(getRequest().getParameter("percentage_"+applist.get(i).getAppnumber())!=null)
			{
				try{
					percentage = Integer.parseInt(getRequest().getParameter("percentage_"+applist.get(i).getAppnumber()));
				}catch(Exception ex){}
			}
			oc.setPercentage(percentage);
			list.add(oc);
		}
		OrderConfigManager.getInstance().saveOrderConfig(list,1,cpinfo.getId());
		return SUCCESS;
	}

	public List<CpInfo> getCplist() {
		return cplist;
	}

	public void setCplist(List<CpInfo> cplist) {
		this.cplist = cplist;
	}

	public List<CpAppInfo> getApplist() {
		return applist;
	}

	public void setApplist(List<CpAppInfo> applist) {
		this.applist = applist;
	}

	public CpInfo getCpinfo() {
		return cpinfo;
	}

	public void setCpinfo(CpInfo cpinfo) {
		this.cpinfo = cpinfo;
	}

	public List<OrderConfig> getList() {
		return list;
	}

	public void setList(List<OrderConfig> list) {
		this.list = list;
	}
	
	
	
}
